package org.black.missedCall2.reply;

import org.black.missedCall2.R;
import org.black.missedCall2.reply.contacts.ContactListActivity;
import org.black.missedCall2.reply.contacts.dataMOdel.ContactDataModel;
import org.black.missedCall2.reply.content.ReplyContentProviderMetaData;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.ContentValues;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.ToggleButton;

public class AddReplyActivity extends Activity {
	private ContactDataModel listenFor = null;
	private ContactDataModel responseTo = null;
	private String message = null;
	private boolean activate = false;

	private ImageButton addFromImageButton = null;

	public void onCreate(Bundle savedInstanceState) {
		if (savedInstanceState != null) {
			if (savedInstanceState
					.getSerializable(ReplyConstant.INTENT_SMS_LISTEN_FOR) != null) {
				this.listenFor = (ContactDataModel) savedInstanceState
						.getSerializable(ReplyConstant.INTENT_SMS_LISTEN_FOR);
			}
			if (savedInstanceState
					.getSerializable(ReplyConstant.INTENT_SMS_RESPONSE_TO) != null) {
				this.responseTo = (ContactDataModel) savedInstanceState
						.getSerializable(ReplyConstant.INTENT_SMS_RESPONSE_TO);
			}
			if (savedInstanceState
					.getSerializable(ReplyConstant.INTENT_SMS_MESSAGE) != null) {
				this.message = savedInstanceState
						.getString(ReplyConstant.INTENT_SMS_MESSAGE);
			}
			this.activate = savedInstanceState
					.getBoolean(ReplyConstant.INTENT_SMS_ACTIVATE);
		}

		super.onCreate(savedInstanceState);
		setContentView(R.layout.reply_add);

		EditText addSmsTextView = (EditText) this
				.findViewById(R.id.addAndEditMissedCallReplyMessageEditText);
		addSmsTextView.clearFocus();
		InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
		imm.hideSoftInputFromWindow(addSmsTextView.getWindowToken(), 0);
		if (this.message != null) {
			addSmsTextView.setText(this.message);
		}

		this.addFromImageButton = (ImageButton) this
				.findViewById(R.id.addFromImageButton);
		this.addFromImageButton.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(final View v) {
				AlertDialog.Builder builder = new AlertDialog.Builder(
						AddReplyActivity.this);
				builder.setTitle(R.string.smsReplyAddFrom).setItems(
						R.array.smsReplyAddFrom, new OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								if (which == 0) {
									Intent intent = new Intent();
									Bundle extras = new Bundle();

									intent.setClass(v.getContext(), ContactListActivity.class);
									extras.putString(ReplyConstant.INTENT_SMS_EDITOR_FOR,
											ReplyConstant.INTENT_SMS_LISTEN_FOR);

									intent.putExtras(extras);
									startActivityForResult(intent,
											ReplyConstant.CODE_GET_CONTACT_INFO);
								}
								// TODO Auto-generated method stub
								System.err.println(which);

							}
						});
				builder.show();

				/*
				 * Intent intent = new Intent(); Bundle extras = new Bundle();
				 * 
				 * intent.setClass(v.getContext(), ContactListActivity.class);
				 * extras.putString(ReplyConstant.INTENT_SMS_EDITOR_FOR,
				 * ReplyConstant.INTENT_SMS_LISTEN_FOR);
				 * 
				 * intent.putExtras(extras); startActivityForResult(intent,
				 * ReplyConstant.CODE_GET_CONTACT_INFO);
				 */
			}
		});
		this.addFromImageButton.requestFocus();

		ImageButton addReplyImageButton = (ImageButton) this
				.findViewById(R.id.addReplyImageButton);
		addReplyImageButton.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent();
				Bundle extras = new Bundle();

				intent.setClass(v.getContext(), ContactListActivity.class);
				extras.putString(ReplyConstant.INTENT_SMS_EDITOR_FOR,
						ReplyConstant.INTENT_SMS_RESPONSE_TO);

				intent.putExtras(extras);
				startActivityForResult(intent,
						ReplyConstant.CODE_GET_CONTACT_INFO);
			}
		});

		Button doneAddReplyButton = (Button) this
				.findViewById(R.id.doneAddReplyButton);
		doneAddReplyButton.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				Uri replySettings = Uri.parse("content://"
						+ ReplyContentProviderMetaData.AUTHORITY + "/"
						+ ReplyContentProviderMetaData.BASE_PATH);
				ContentValues values = new ContentValues();
				values.put(ReplyContentProviderMetaData.COLUMN_LISTEN_FOR,
						listenFor.getDataId());
				values.put(ReplyContentProviderMetaData.COLUMN_RESPONSE_TO,
						responseTo.getDataId());
				values.put(ReplyContentProviderMetaData.COLUMN_MESSAGE, message);
				values.put(ReplyContentProviderMetaData.COLUMN_SHOULD_REPLY,
						activate);
				getContentResolver().insert(replySettings, values);
				Log.i(ReplyConstant.REPLY_TAG, "Add reply list!");
			}
		});

		Button cancelAddReplyButton = (Button) this
				.findViewById(R.id.cancelAddReplyButton);
		cancelAddReplyButton.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
				Log.i(ReplyConstant.REPLY_TAG, "Back!");
			}
		});
		ToggleButton activateReplyToggleButton = (ToggleButton) this
				.findViewById(R.id.activateReplyToggleButton);
		if (this.activate == true) {
			activateReplyToggleButton.setChecked(true);
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (data != null) {
			String editorFor = data
					.getStringExtra(ReplyConstant.INTENT_SMS_EDITOR_FOR);

			Object contactDataModelObject = data
					.getSerializableExtra(ReplyConstant.BUNDLE_KEY_SELECTED_CONTACT);
			if (TextUtils.isEmpty(editorFor) == false) {
				if (contactDataModelObject != null) {
					ContactDataModel contactDataModel = (ContactDataModel) contactDataModelObject;
					if (ReplyConstant.INTENT_SMS_LISTEN_FOR.equals(editorFor) == true) {
						this.listenFor = contactDataModel;
					} else if (ReplyConstant.INTENT_SMS_RESPONSE_TO
							.equals(editorFor) == true) {
					} else {
						// TODO: severe problem
					}
				}

			}
			super.onActivityResult(requestCode, resultCode, data);
		}
	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		if (this.listenFor != null) {
			outState.putSerializable(ReplyConstant.INTENT_SMS_LISTEN_FOR,
					this.listenFor);
		}
		if (this.responseTo != null) {
			outState.putSerializable(ReplyConstant.INTENT_SMS_RESPONSE_TO,
					this.responseTo);
		}
		if (this.message != null) {
			outState.putString(ReplyConstant.INTENT_SMS_MESSAGE, message);
		}
		outState.putBoolean(ReplyConstant.INTENT_SMS_ACTIVATE, this.activate);

		super.onSaveInstanceState(outState);
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
	}

	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
	}

	private void setSelectedContactDataModelAndTextView(TextView textView,
			ContactDataModel contactDataModel) {
		if (textView == null || contactDataModel == null) {
			Log.e(ReplyConstant.REPLY_TAG, "Some parameter is null!");
			return;
		}

		String name = contactDataModel.getName();
		String phoneNumber = contactDataModel.getPhoneNumber();
		if (TextUtils.isEmpty(phoneNumber)) {
			textView.setText(name.trim() + phoneNumber.toString());
		}
	}

}
